$(function() {
  'use strict';
  //获取带有data-rule属性的input
  //解析每个Input的验证规则
  //通过底层validator 在input blur时验证input输入的值是否与规则匹配
  //rule={min: 18, max: 99, maxlength: 16, minlength: 4 ..};
  window.Validator = function(val, rule) {
    this.is_valid = function(new_val) {
      if (new_val !== undefined)
        val = new_val;
      if (!val && !rule.required)
        return true;
      for (var key in rule) {
        if (key == 'required')
          continue;
        var result = this['validate_' + key]();
        if (!result)
          return false;
      }
      return true;
    };
    this.validate_max = function() {
      val = parseFloat(val);
      return val <= rule.max;
    };
    this.validate_min = function() {
      val = parseFloat(val);
      return val >= rule.min;
    };
    this.validate_maxlength = function() {
      val = val.toString();
      return val.length <= rule.maxlength;
    };
    this.validate_minlength = function() {
      val = val.toString();
      return val.length >= rule.minlength;
    };
    this.validate_numeric = function() {
      return $.isNumeric(val);
    };
    this.validate_pattern = function() {
      var reg = new RegExp(rule.pattern);
      return reg.test(val);
    };
    this.validate_required = function() {
      var real = $.trim(val);
      if (!real && real !== 0)
        return false;
      else
        return true;
    };
  }
});
